package com.njtoyo.taxi.admin.logic.allowance;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.njtoyo.taxi.admin.mapper.master.entity.Recommendation;
import com.njtoyo.taxi.admin.mapper.master.enums.RecommendationPromoteChannel;
import com.njtoyo.taxi.admin.mapper.master.mapper.RecommendationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service

public class RecommendationLogic {

    @Autowired
    private RecommendationMapper recommendationMapper;

    public Integer getCount(String startTime, String endTime) {
        List<RecommendationPromoteChannel> type = new ArrayList<>();
        type.add(RecommendationPromoteChannel.user);
        type.add(RecommendationPromoteChannel.driver);

        // 获取ride_order数量
        return getCountByPromoteChannel(startTime, endTime, type);
    }

    // 司机推广量
    public Integer getDriverCount(String startTime, String endTime) {
        List<RecommendationPromoteChannel> type = new ArrayList<>();
        type.add(RecommendationPromoteChannel.driver);

        return getCountByPromoteChannel(startTime, endTime, type);
    }

    // 司机推广量
    public Integer getDriverCount(Date startTime, Date endTime) {
        String startTimeStr = DateUtil.formatDateTime(startTime);
        String endTimeStr = DateUtil.formatDateTime(endTime);

        List<RecommendationPromoteChannel> type = new ArrayList<>();
        type.add(RecommendationPromoteChannel.driver);

        return getCountByPromoteChannel(startTimeStr, endTimeStr, type);
    }

    // 乘客推广量
    public Integer getUserCount(String startTime, String endTime) {
        List<RecommendationPromoteChannel> type = new ArrayList<>();
        type.add(RecommendationPromoteChannel.user);

        return getCountByPromoteChannel(startTime, endTime, type);
    }

    private Integer getCountByPromoteChannel(String startTime, String endTime, List<RecommendationPromoteChannel> type) {
        return new LambdaQueryChainWrapper<>(recommendationMapper)
                .in(Recommendation::getPromoteChannel, type)
                .between(Recommendation::getCreatedAt, startTime, endTime)
                .count();
    }
}
